<template>
  <div>
    <el-table
      :data="records"
      border
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        width="80"
      >
      </el-table-column>
      <el-table-column
        label="名称"
        prop="skuName"
        width="80"
      >
      </el-table-column>
      <el-table-column
        label="描述"
        prop="skuDesc"
      >
      </el-table-column>
      <el-table-column
        #default="{row}"
        label="默认图片"
        prop="address"
      >
        <img :src="row.skuDefaultImg" style="width: 80px; height: 80px">
      </el-table-column>
      <el-table-column
        label="重量"
        prop="weight"
      >
      </el-table-column>
      <el-table-column
        label="价格"
        prop="price"
      >
      </el-table-column>
      <el-table-column
        #default="{row}"
        label="操作"
        prop="address"
      >
        <el-button v-if="row.isSale==0" icon="el-icon-sort-down" size="mini" type="success" @click="sale(row)"
        ></el-button>
        <el-button v-else icon="el-icon-sort-up" size="mini" type="success" @click="cancel(row)"></el-button>
        <el-button icon="el-icon-edit" size="mini" type="primary" @click="edit"></el-button>
        <el-button icon="el-icon-info" size="mini" type="info" @click="getSkuInfo(row)"></el-button>
        <el-button icon="el-icon-delete" size="mini" type="danger"></el-button>


      </el-table-column>
    </el-table>


    <el-pagination
      :current-page="page"
      :page-size="limit"
      :page-sizes=[3,5,10]
      :total="total"
      layout="prev, pager, next, jumper, ->, sizes, total"
      style="margin-top:20px; text-align: center"
      @current-change="getSkuList"
      @size-change="handleSizeChange"
    >
    </el-pagination>


    <el-drawer
      :show-close="false"
      :visible.sync="show"
      size="50%"
    >
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{ skuInfo.skuName }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{ skuInfo.skuDesc }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{ skuInfo.price }}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="16">
          <el-tag v-for="(attr, index) in skuInfo.skuAttrValueList" :key="attr.id" style="margin-right: 10px"
                  type="success"
          >
            {{ attr.attrId }}:{{ attr.valueId }}
          </el-tag>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
          <el-carousel height="150px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img :src="item.imgUrl" alt="">
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>


  </div>
</template>

<script>
export default {
  name: 'Sku',
  data() {
    return {
      page: 1,
      limit: 10,
      records: [],
      total: 0,
      skuInfo: {},
      show: false
    }
  },
  mounted() {
    this.getSkuList()
  },
  methods: {
    async getSkuList(pages = 1) {
      this.page = pages
      const { page, limit } = this
      let result = await this.$API.sku.reqSkuList(page, limit)
      if (result.code == 200) {
        this.total = result.data.total
        this.records = result.data.records
      }
    },
    handleSizeChange(limit) {
      this.limit = limit
      this.getSkuList()
    },
    async sale(row) {
      let result = await this.$API.sku.reqSale(row.id)
      if (result.code == 200) {
        row.isSale = 1
        this.$message({ type: 'success', message: '上架成功' })
      }
    },
    async cancel(row) {
      let result = await this.$API.sku.reqCancel(row.id)
      if (result.code == 200) {
        row.isSale = 0
        this.$message({ type: 'success', message: '下架成功' })
      }
    },
    edit() {
      this.$message('正在开发中。。。。')
    },
    async getSkuInfo(sku) {
      let result = await this.$API.sku.reqSkuById(sku.id)
      this.show = true
      if (result.code == 200) {
        this.skuInfo = result.data
      }
    }

  }
}
</script>

<style>
.el-row .el-col-5 {
  font-size: 10px;
  text-align: right;
}

.el-col {
  margin: 10px 10px;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}

</style>
